package cn.yunyichina.provider.message.alipay.utils;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.yunyichina.provider.message.alipay.entity.ImageMessage;
import cn.yunyichina.provider.message.alipay.entity.MsgTemplateContent;
import cn.yunyichina.utils.commons.StringUtils;

/**
 * 消息构造工具
 * 
 * @author baoxing.gbx
 * @version $Id: AlipayMsgBuildUtil.java, v 0.1 Jul 24, 2014 5:47:19 PM
 *          baoxing.gbx Exp $
 */
public class AlipayMessageBuildUtils {
	public static String basePath = "";//basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/";
	private static Logger logger = LoggerFactory.getLogger(AlipayMessageBuildUtils.class);

	/**
	 * 构造单发/群发单图文消息
	 * 
	 * @param fromUserId 接收人ID，如果为空或null，则构建群发消息
	 * @param desc 备注
	 * @param imgsrc 图片src
	 * @param title 标题
	 * @param url 点击后转向的URL
	 * @return 如果fromUserId为空或null，则构建群发消息
	 */
	public static String buildImageTextMsg(String fromUserId, String desc, String imgsrc, String title, String url) {
		JSONObject reqStr = new JSONObject();
		JSONArray articles = new JSONArray();
		JSONObject article = new JSONObject();
		article.put("actionName", "立即查看");
		article.put("desc", desc);
		article.put("imageUrl", basePath + imgsrc);
		article.put("title", title);
		article.put("url", url);
		articles.add(article);
		reqStr.put("articles", articles);
		reqStr.put("msgType", "image-text");
		
		if ( ! StringUtils.isBlank(fromUserId)) {
			reqStr.put("toUserId", fromUserId);
		}
		
		return reqStr.toJSONString();
	}

	

	/**
	 * 构造单发/群发纯文本消息
	 * 
	 * @param fromUserId 接收人ID  如果fromUserId为空或null，则构建群发消息
	 * @param textContent
	 * @return  如果fromUserId为空或null，则构建群发消息
	 */
	public static String buildTextMsg(String fromUserId, String textContent) {
		JSONObject reqStr = new JSONObject();
		JSONObject text = new JSONObject();
		text.put("content", textContent);
		if ( ! StringUtils.isBlank(fromUserId)) {
			reqStr.put("toUserId", fromUserId);
		}
		
		reqStr.put("msgType", "text");
		reqStr.put("text", text);
		return reqStr.toJSONString();
	}

	/**
	 * 截取字符串方式组支付宝模版消息
	 * 
	 * @param fromUserId 接收人ID
	 * @param templateId 模板ID
	 * @param content 内容
	 * @param url 点击后转向的URL
	 * @param topColor 标题头颜色
	 * @param remark 备注
	 * @return
	 */
	public static String buildTemplateMsg(String fromUserId, String templateId, List<MsgTemplateContent> msgTemplateContents, String url, String topColor) {
		JSONObject reqStr = new JSONObject();
		JSONObject context = new JSONObject();
		JSONObject template = new JSONObject();
		JSONObject keyword = null;
		for (MsgTemplateContent m : msgTemplateContents) {
			keyword = new JSONObject();
			keyword.put("value", m.getValue());
			keyword.put("color", m.getFontColor());
			context.put(m.getNode(), keyword);
		}
		context.put("headColor", topColor);
		context.put("url", url);
		context.put("actionName", "查看详情");
		template.put("templateId", templateId);
		template.put("context", context);
		reqStr.put("toUserId", fromUserId);
		reqStr.put("template", template);
		return reqStr.toJSONString();
	}


	/**
	 * 构造基础的响应消息
	 * 
	 * @return
	 */
	public static String buildBaseAckMsg(String fromUserId, String appId) {
		StringBuilder sb = new StringBuilder();
		sb.append("<XML>");
		sb.append("<ToUserId><![CDATA[" + fromUserId + "]]></ToUserId>");
		sb.append("<AppId><![CDATA[" + appId + "]]></AppId>");
		sb.append("<CreateTime>" + Calendar.getInstance().getTimeInMillis() + "</CreateTime>");
		sb.append("<MsgType><![CDATA[ack]]></MsgType>");
		sb.append("</XML>");
		return sb.toString();
	}
	/**
	 * 创建多/单图文消息
	 * @param openId 接收人ID
	 * @param listImageMessage 图文消息列表
	 * @return
	 */
	public static String buildMultiImgTextMsg(String openId, List<ImageMessage> listImageMessage) {
		JSONObject reqStr = new JSONObject();
		reqStr.put("msgType", "image-text");
		reqStr.put("toUserId", openId);
		JSONArray articles = new JSONArray();
		JSONObject article = null;
		if (listImageMessage != null && listImageMessage.size() > 0) {
			for (ImageMessage m : listImageMessage) {
				article = new JSONObject();
				article.put("actionName", "立即查看");
				article.put("desc", m.getDesc());
				article.put("imageUrl",m.getImageUrl());
				article.put("title", m.getTitle());
				article.put("url", m.getUrl());
				articles.add(article);
			}
		}
		reqStr.put("articles", articles);
		return reqStr.toJSONString();

	}
	
	
	/**
	 * 匹配推送biz_content拼装
	 * 
	 * @param mobileNo
	 * @param templateId
	 * @return
	 */
	public static String buildTemplateMsg(String mobileNo, String templateId) {
		JSONObject root = new JSONObject();
		JSONObject matcher = new JSONObject();
		JSONObject template = new JSONObject();
		matcher.put("mobileNo", mobileNo);
		template.put("templateId", templateId);
		root.put("matcher", matcher);
		root.put("template", template);
		return root.toJSONString();
	}
	public static void main(String[] args) {

//		ImageMessage meterial = new ImageMessage() ;
//		meterial.setImageSrc("http://example.com/abc.jpg");
//		meterial.setLink("https://www.example.com/a.php");
//		meterial.setDescription("图文内容");
//		meterial.setTitle("标题");
//
//		m2.setImageSrc("http://example.com/2343.jpg");
//		m2.setLink("https://www.example.com/23423423.php");
//		m2.setDescription("图文内容2");
//		m2.setTitle("标题2");
//		m1.setImageSrc("http://example.com/adsfdsc.jpg");
//		m1.setLink("https://www.example.com/sdafsd.php");
//		m1.setDescription("图文内容3");
//		m1.setTitle("标题3");
//		m3.setImageSrc("http://example.com/2343bc.jpg");
//		m3.setLink("https://www.example.com/2332.php");
//		m3.setDescription("图文内容4");
//		m3.setTitle("标题4");
//		subMixedMeterialList.add(m1);
//		subMixedMeterialList.add(m2);
//		subMixedMeterialList.add(m3);
//		meterial.setSubMixedMeterialList(subMixedMeterialList);
//		System.out.println(buildMultiImgTextMsg("R8qtKrh1dS49uWaLZUbPr0re8hjWnYClscK-vjUi4qOHCIhO5aJXi4frP7yHEJoJ01", meterial));

		/*String s = buildSingleImgTextMsg("asdasdasdsadadadasdasdas", "hahahahahahahaha", "http://ss.sd.casd", "adasdasdas", "dasddsfadasfdas");
		System.out.println(s);*/
	}


}
